package com.blogs.ui;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.blogs.component.BaseActivity;
import com.blogs.component.CommentQueueAdapter;
import com.blogs.component.GetMoreListView;
import com.blogs.entity.CommentQueueFeed;
import com.blogs.service.SendCommentService;
import com.blogs.tools.DBSharedPreferences;
import com.blogs.tools.LoadingGif;
import com.blogs.tools.TbCommentQueue;
import com.cnblogs.R;

public class CommentQueueActivity extends BaseActivity implements OnItemClickListener,OnClickListener{
	enum getDataMethod{
		init,//初始化
		more,//点击更多
	}//获取数据方式
	private RelativeLayout title_top_bg;
	private TbCommentQueue tb;
	private DBSharedPreferences sp;
	private Toast toast;
	private Button title_include_back;
	private TextView title_include_title;
	private GetMoreListView listView;
	private LoadingGif loadingGif;
	private LinearLayout queue_list_body;
	private CommentQueueAdapter adapter;
	private ArrayList<CommentQueueFeed> feedList;
	private CommentQueueReceiver commentQueueReceiver;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.comment_queue);
		sp= new DBSharedPreferences(this);
		ViewGroup headview=(ViewGroup)findViewById(R.id.main_title_include);
		title_top_bg=(RelativeLayout)headview;
		title_include_title=(TextView)headview.findViewById(R.id.title_include_title);
		title_include_back=(Button)headview.findViewById(R.id.title_include_back);
		title_include_title.setText("评论队列");
		title_include_back.setVisibility(View.VISIBLE);
		title_include_back.setOnClickListener(this);
		queue_list_body=(LinearLayout)findViewById(R.id.comment_queue_body);
		listView=new GetMoreListView(this);
		listView.setOnItemClickListener(this);
//		listView.setonMoreListener(new OnMoreListener() {
//			
//			public void onGetMore() {
//				// TODO Auto-generated method stub
//				
//			}
//		});
		listView.setFootNotMore();
		
		loadingGif=new LoadingGif(this, listView, queue_list_body,sp);
		loadingGif.setOnReloadListener(new LoadingGif.OnReloadListener() {
			
			public void onClick() {
				// TODO Auto-generated method stub
				requestData();
			}
		});
		
		commentQueueReceiver=new CommentQueueReceiver();//设置广播接收
		IntentFilter skinFilter = new IntentFilter();
        skinFilter.addAction("CommentQueueReceiver");
        registerReceiver(commentQueueReceiver, skinFilter);
        
		this.requestData();
		this.InitSkin();
	}
	private void InitSkin()
	{
		if(sp.GetIsDarkState())
		{
			title_top_bg.getBackground().setLevel(2);
			queue_list_body.getBackground().setLevel(2);
		}
		else
		{
			title_top_bg.getBackground().setLevel(1);
			queue_list_body.getBackground().setLevel(1);
		}
	}
	private void requestData()
	{
		loadingGif.showLoading();
		this.sendTimeLine(getDataMethod.init);
	}
	public void updateRequestData(getDataMethod gdm)
	{
		if(adapter!=null)
		{
			adapter.notifyDataSetChanged();
			if(gdm==getDataMethod.init)
			{
				listView.setAdapter(adapter);
			}
		}
		else
		{
			adapter=new CommentQueueAdapter(this, feedList);
			listView.setAdapter(adapter);
		}

		loadingGif.showData();
	}
	private void sendTimeLine(final getDataMethod gdm)
	{
		tb=new TbCommentQueue(this);
		if(feedList==null)
			feedList=new ArrayList<CommentQueueFeed>();
		if(gdm==getDataMethod.init)
		{
			feedList.clear();
		}
		ArrayList<CommentQueueFeed> feeds=tb.Select();
		for(int i=0;i<feeds.size();i++)
		{
			feedList.add(feeds.get(i));
		}
		updateRequestData(gdm);
		tb.DbClose();
	}
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v==title_include_back)
		{
			finish();
		}
	}
	public void onItemClick(AdapterView<?> parent, View view,final int position,
			long id) {
		// TODO Auto-generated method stub
		if(position>feedList.size())
			return;
		String[] items = { "重发", "删除", "取消" };
		new AlertDialog.Builder(this).setItems(items, new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				switch (which) {
				case 0:
					Intent intent=new Intent(CommentQueueActivity.this,SendCommentService.class);
					intent.putExtra("sid", sp.GetSessionID());
					intent.putExtra("type", feedList.get(position).type);
					intent.putExtra("niceNmae", sp.GetNickName());
					intent.putExtra("comment_id", feedList.get(position).comment_id);
					intent.putExtra("parentCommentID", feedList.get(position).parentCommentID);
					intent.putExtra("commendBody",feedList.get(position).commendBody);
					intent.putExtra("blogapp", feedList.get(position).blogapp);
					Log.i("blogapp", "dd"+feedList.get(position).blogapp);
			        startService(intent);
			        TbCommentQueue tb=new TbCommentQueue(CommentQueueActivity.this);
			        tb.Delete(feedList.get(position).id+"");
			        tb.DbClose();
					feedList.remove(feedList.get(position));
					adapter.notifyDataSetChanged();
					showToast("评论已进入发送队列");
					break;
				case 1:
					new AlertDialog.Builder(CommentQueueActivity.this)  
					.setTitle("删除草稿") 
					.setMessage("是否删除该草稿？\n删除后将不能恢复") 
					.setPositiveButton("确定", new DialogInterface.OnClickListener() {
						
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							TbCommentQueue tbd=new TbCommentQueue(CommentQueueActivity.this);
							tbd.Delete(feedList.get(position).id+"");
							tbd.DbClose();
							feedList.remove(feedList.get(position));
							adapter.notifyDataSetChanged();
						}
					}) 
					.setNegativeButton("取消", null) 
					.show();
					
					break;
				case 2:
					break;
				default:

					break;
				}
			}
		}).create().show();
	}
	private void showToast(String text)
	{
		if (toast == null) {
			toast = Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT);
		} else {
			toast.setText(text);
			toast.setDuration(Toast.LENGTH_SHORT);
		}
		toast.show();
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		unregisterReceiver(commentQueueReceiver);  
	}
	class CommentQueueReceiver extends BroadcastReceiver
	{
		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			Log.i("CommentQueueReceiver", "ok");
			requestData();
			listView.setSelection(0);
		}
	}
}
